package org.itboys.weixin.web.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.weixin.entity.WeixinUser;
import org.itboys.weixin.service.WeixinUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping(value = "/weixin/user")
public class WeixinUserController extends BaseController {

	@Autowired
	private WeixinUserService weixinUserService;
	@Autowired
	private ResourceHolder resourceHolder;

	@RequestMapping("/vm/{weixinId}")
	public ModelAndView projectMenus(@PathVariable("weixinId") Long weixinId,
			HttpServletRequest request, Model model) {
		model.addAttribute("weixinId", weixinId);
		return new ModelAndView("/weixin/user/list");
	}

	@RequestMapping("/list")
	public void list(HttpServletResponse response, HttpServletRequest request) {
		Page<WeixinUser> page = weixinUserService.pageQuery(request);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}

	@RequestMapping("/getById")
	public ModelAndView getById(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String openid = request.getParameter("openid");
		String weixinId = request.getParameter("weixinId");
		model.addAttribute("weixinId", weixinId);
		if (StringUtils.isNotBlank(openid)) {
			WeixinUser entity = weixinUserService.getWeixinUser(
					Long.valueOf(weixinId), openid);
			model.addAttribute("entity", entity);
		}
		/*model.addAttribute("staticRoot",resourceHolder.getStringValue("staticRoot"));
		model.addAttribute("adminRoot",resourceHolder.getStringValue("adminRoot"));
		model.addAttribute("imgRoot",resourceHolder.getStringValue("imgRoot"));*/
		
		return new ModelAndView("/weixin/user/input");
	}

	@RequestMapping("/save")
	public void insert(HttpServletResponse response,
			@ModelAttribute WeixinUser weixinUser) {
		try {
			if (weixinUser.getOpenid() == null) {
				weixinUserService.save(weixinUser);
			} else {
				weixinUserService.update(weixinUser);
			}
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		} catch (Exception e) {
			logger.error("weixinuser insert error", e);
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	@RequestMapping("/delete")
	public void delete(HttpServletResponse response, HttpServletRequest request) {
		try {
			Long id = Long.valueOf(request.getParameter("id"));
			AjaxUtils.renderText(response,
					String.valueOf(weixinUserService.delete(id)));
		} catch (Exception e) {
			logger.error("weixinuser delete error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}

	@RequestMapping("/change")
	public void change(HttpServletResponse response, HttpServletRequest request) {
		try {
			Long id = Long.valueOf(request.getParameter("id"));
			String isTest = request.getParameter("isTest");
			WeixinUser weixinUser = new WeixinUser();
			weixinUser.setId(id);
			weixinUser.setIsTest(Integer.valueOf(isTest));
			weixinUserService.update(weixinUser);
			AjaxUtils.renderJson(response, 1);
		} catch (Exception e) {
			logger.error("weixinuser change error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}

	@RequestMapping("/doSyncWeixinUserList")
	public void doSyncWeixinUserList(HttpServletResponse response,
			HttpServletRequest request) {
		try {
			String weixinId = request.getParameter("weixinId");
			weixinUserService.doSyncWeixinUserList(Long.valueOf(weixinId));
			AjaxUtils.renderJson(response, 1);
		} catch (Exception e) {
			logger.error("doSyncWeixinUserList error", e);
			AjaxUtils.renderJson(response, 0);
		}
	}

}
